<?php 
class uploadImage{
	
	#上传的文件名
	public static $filename='category';
	
	#小图片前缀
	public static $small='small_';
	
	#水印图片前缀
	public static $sy='sy_';
	
	
	#是否产生小图片
	public static $smallimg=true;
	
	#是否产生水印
	public static $syimg=true;
	
	#上传地址
	public static $dir='';
	
	#上传图片类
	public static $uploadClass;
	
	#图片宽度
	public static $width;
	
	public static $height;
	
	 
	/**
	 * 图片上传，产生小图片，返回图片名称组成的数组
	 *
	 * @return array
	 */
	static public   function upload(){
	    self::config();
		$newAry=array();
		$bigimage=self::$uploadClass->upload(self::$filename);
		if(!$bigimage){
			return false;
		}
		$smallimage=self::$small.$bigimage;
		$syimage=self::$sy.$bigimage;
		if(!is_array($bigimage)){
			$smallimgPath=DOCUMENT_ROOT.'/'.self::$dir.'/'.self::$small.$bigimage;
			$bigimagePath=DOCUMENT_ROOT.'/'.self::$dir.'/'.$bigimage;
			if(!is_file($bigimagePath)){
				return false;
			}
			if(self::$smallimg){
				$smallClass=new images($bigimagePath);
				$smallClass->set_attribute('width',self::$width);
				$images=$smallClass->output();			
				$tmp=fopen($smallimgPath,'w');
				fwrite($tmp,$images);
				fclose($tmp);
			}
			@unlink($bigimagePath);
			self::$dir=str_replace('www/','',self::$dir);
			$newAry=array('/'.self::$dir.'/'.$bigimage);
			
		}else{
			foreach ($bigimage as $val){			
				$smallimg=DOCUMENT_ROOT.'/'.self::$dir.'/'.self::$small.$val;	
				$roodir=DOCUMENT_ROOT.'/'.self::$dir.'/'.$val;
				if(!is_file($roodir)){
					return false;
					continue;
				}else{
					if(self::$smallimg){
						$smallClass=new images($roodir);
						$smallClass->set_attribute('width',self::$width);
						$images=$smallClass->output();
						$tmp=fopen($smallimg,'w');
						fwrite($tmp,$images);
						fclose($tmp);
					}
				}
				@unlink($roodir);
				self::$dir=str_replace('www/','',self::$dir);
				$newAry[]='/'.self::$dir.'/'.$val;
			}
		}
		//var_dump($newAry);
		return $newAry;
	}
	#上传其它文件
	public static function upload_file(){
	    self::config();
		$bigimage=self::$uploadClass->upload(self::$filename);
		if(!$bigimage){
			return false;
		}
		self::$dir=str_replace('www/','',self::$dir);
		return '/'.self::$dir.'/'.$bigimage;		
	}
	
	/**
	 * 加载配置信息
	 *
	 */
	public static  function config(){
	    self::$dir=&get_config('upload_dir');
		self::$smallimg=&get_config('smalling');
		self::$syimg=&get_config('syimg');
		self::$width=&get_config('width');
		self::$height=&get_config('height');
		self::$uploadClass=new upload();	
	}
	
	/**
	 * 删除图片
	 *
	 * @param string $simg
	 * @param string $eimg 在编辑提交后对比是否同一个图片,不是则删除
	 * @return bool
	 */
	public static  function delimg($simg,$eimg=''){
		$return=true;
		if(!is_file($simg)){
			$simg=DOCUMENT_ROOT.'/www'.$simg;
			$eimg=DOCUMENT_ROOT.'/www'.$eimg;
		}
		if(is_file($simg)){
			if($simg!=$eimg){
				@unlink($simg);
			}
		}
		$smallimg=self::exFilename($simg);
		if(is_file(DOCUMENT_ROOT.$smallimg)){
			$return=@unlink(DOCUMENT_ROOT.$smallimg);
		}		
		return $return;
	}
	
	/**
	 * 得到小图片地址
	 *
	 * @param string $bigname 大图片地址
	 * @return string
	 */
	public function exFilename($bigname){
		self::config();
		$name=basename($bigname);
		return '/'.self::$dir.'/'.self::$small.$name;
	}
	
	public function smallUrl($bigname){
		self::config();
		$name=basename($bigname);
		 $src='/'.self::$dir.'/'.self::$small.$name;
		 $src=str_replace('www/','',$src);
		 return $src;
	}	
	
	public function copyFile($y){
	    self::config();
	    $name=basename($y);
	    $Ary=explode('.',$name);
	    $num=count($Ary);
	    $sname=$Ary[$num-2].'1.'.$Ary[$num-1];	    
	    if(is_file(DOCUMENT_ROOT.'/www/'.$y)){
	        if(copy(DOCUMENT_ROOT.'/www/'.$y,DOCUMENT_ROOT.'/'.self::$dir.'/'.$sname)){
	            $dir=str_replace('www/','',self::$dir);
	            return '/'.$dir.'/'.$sname;
	        }else{
	            debug(DOCUMENT_ROOT.'/'.self::$dir.'/'.$sname);
	            return false;
	        }
	    }else{
	        return false;
	    }
	}

	/**
	 * 图片显示,找不到图片，则以"无图片"显示
	 *
	 * @param string $img
	 * @return string
	 */
	public static  function imgsrc($img){
		self::$dir=&get_config('upload_dir');

		if(!is_file($img)){
			if(!is_file(DOCUMENT_ROOT.'/www'.$img)){
				$img= '/images/holife_logo.gif';
			}
		}

		return $img;
	}
}